package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService iRoleService;

    @RequestMapping("/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageResult result = service.query(qo);
        model.addAttribute("result", result);
        List<Department> depts = departmentService.getAll();
        model.addAttribute("depts",depts);
        return "employee/list";
    }
    @RequestMapping("/input")
    @RequiredPermission({"员工编辑","employee:input"})
    public String input(Model model,Long id){
        if (id != null){
            Employee employee = service.get(id);
            model.addAttribute("entity",employee);
        }
        List<Department> depts = departmentService.getAll();
        model.addAttribute("depts",depts);
        List<Role> roles = iRoleService.getAll();
        model.addAttribute("roles",roles);

        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"员工修改","employee:saveOrUpdate"})
    public  String saveOrUpdate(Employee employee,Long[] ids){
       service.saveOrUpdate(employee,ids);
       return "redirect:/employee/list.do";
    }
    @RequiredPermission({"员工删除","employee:delete"})
    @RequestMapping("/delete")
    public  String delete(Long id){
        service.delete(id);
        return "redirect:/employee/list.do";
    }
}
